/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.model;

import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.guts.gui.action.GutsAction;
import at.tugraz.kmi.css.cbkst2.PERSISTENCE_TYPE;
import at.tugraz.kmi.css.cbkst2.prereq.structurevis.CompetenceManagerModel;
import at.tugraz.kmi.css.cbkst2.prereq.structurevis.FileFormatError;

import com.google.inject.Inject;

public class StructureModel {

	private final JFileChooser				jFileChooser1;
	private final CompetenceManagerModel	model;

	private final GutsAction				newStructureAction;
	private final GutsAction				saveStructureAction;
	private final GutsAction				loadStructureAction;
	private final GutsAction				loadICBAction;

	@Inject
	public StructureModel(final CompetenceManagerModel model) {
		this.model = model;
		jFileChooser1 = new JFileChooser();
		for (PERSISTENCE_TYPE loaderTYPE : model.getAvailablePersistenceTypes()) {
			FileNameExtensionFilter fne = new FileNameExtensionFilter(
					loaderTYPE.getDescription(), loaderTYPE.toString());
			jFileChooser1.addChoosableFileFilter(fne);
		}

		// INIT Actions
		saveStructureAction = new GutsAction("saveStructure") {
			@Override
			protected void perform() {
				jFileChooser1.setName("Structure File Saver");
				jFileChooser1.setDialogType(JFileChooser.SAVE_DIALOG);
				jFileChooser1.showSaveDialog(null);
				File selectedFile = jFileChooser1.getSelectedFile();
				saveStructure(selectedFile);
			}

		};
		loadStructureAction = new GutsAction("loadStructure") {
			@Override
			protected void perform() {
				jFileChooser1.setName("Structure File Chooser");
				jFileChooser1.showOpenDialog(null);
				try {
					File file = jFileChooser1.getSelectedFile();
					model.loadStructureFromExtension(file);
				} catch (FileFormatError e) {
					throw new IllegalArgumentException("Could not load File!",
							e);
				} catch (FileNotFoundException e) {
					throw new IllegalArgumentException("Could not load File!",
							e);
				}
			}
		};

		newStructureAction = new GutsAction("newStructure") {
			@Override
			protected void perform() {
				model.newStructure();
			}

		};
		loadICBAction = new GutsAction("loadICB") {
			@Override
			protected void perform() {
				loadICB();
			}

		};
	}

	void saveStructure(File selectedFile) {
		try {
			model.saveStructureFromExtension(selectedFile);
		} catch (FileFormatError e) {
			FileNameExtensionFilter filter = (FileNameExtensionFilter) jFileChooser1
					.getFileFilter();
			String[] s = filter.getExtensions();
			try {
				String extension = s[0];
				File f = new File(selectedFile + "" + extension);
				model.saveStructureFromExtension(f);
			} catch (FileFormatError e1) {
				throw new IllegalStateException(e1);
			}
		}
	}

	void loadICB() {
		model.loadStructure(getClass().getResourceAsStream("/icb.graphml"),
				PERSISTENCE_TYPE.graphml);
	}

	// ------ Actions Accessors. public GutsAction getLoadICBAction() {

	public GutsAction getNewStructureAction() {
		return newStructureAction;
	}

	public GutsAction getSaveStructureAction() {
		return saveStructureAction;
	}

	public GutsAction getLoadStructureAction() {
		return loadStructureAction;
	}

	public GutsAction getLoadICBAction() {
		return loadICBAction;
	}

}
